package com.cstor.liang.web.controller;

import javax.validation.Valid;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cstor.liang.pojo.entity.Region;
import com.cstor.liang.pojo.other.RegionVo;
import com.cstor.liang.pojo.other.ResultJo;
import com.cstor.liang.service.RegionService;
import com.cstor.liang.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value = "/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    @RequestMapping(value = "/province", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResultJo province() {
        //获取全部省份
        return ResultJo.returnOk(regionService.getAllProvince());
    }

    @RequestMapping(value = "/city", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResultJo city(@Valid RegionVo region, BindingResult error) {
        //数据校验
        if (error.hasErrors()) {
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        //获取指定省份的所有城市
        return ResultJo.returnOk(regionService.getAllCity(region.getId()));
    }

    @RequestMapping(value = "/county", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResultJo county(@Valid RegionVo region, BindingResult error) {
        //数据校验
        if (error.hasErrors()) {
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        //获取指定城市的所有县
        return ResultJo.returnOk(regionService.getAllCounty(region.getId()));
    }

    @RequestMapping(value = "/street", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResultJo street(@Valid RegionVo region, BindingResult error) {
        //数据校验
        if (error.hasErrors()) {
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        //获取指定县的所有街道
        return ResultJo.returnOk(regionService.getAllStreet(region.getId()));
    }

    @RequestMapping(value = "/countyAndStreet", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public JSONObject countyAndStreet(@Valid RegionVo region, BindingResult error) {
        JSONObject result = new JSONObject();
        //数据校验
        if (error.hasErrors()) {
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            result.put("code", 1);
            result.put("message", message);
            return result;
        }

        //获取指定城市的所有县和街道
        List<Region> list = regionService.getCountyAndStreet(region.getId());
        JSONArray jsonArray = new JSONArray();
        //过滤得到所有县
        list.stream()
                .filter(p -> p.getStage() == RegionService.COUNTY)
                //将 Region 转成 JSONObject
                .map(q -> JSONObject.parseObject(JSON.toJSONString(q)))
                .forEach(g -> jsonArray.add(g));
        //给每个县添加对应的街道
        for (Object o : jsonArray) {
            JSONObject tempObject = (JSONObject) o;
            JSONArray tempList = new JSONArray();
            //过滤得到指定县的所有街道
            list.stream()
                    .filter(p -> p.getRegionCode().startsWith(
                            tempObject.get("regionCode").toString().substring(0, 6))
                            && p.getStage() == RegionService.STREET)
                    .forEach(g -> tempList.add(g));
            //将县与对应的街道绑定
            tempObject.put("street", tempList);
        }

        result.put("code", 0);
        result.put("data", jsonArray);
        return result;
    }

}
